package uniandes.caing.ccambios.entidades;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

@SuppressWarnings("unused")
public class SourceFile {

	private ArrayList<Line> lines;
	private ArrayList<ChangeHeader> changeHeader;
	private int version;
	private String name;
	private String filePath;
	private int locTotal;
	private boolean changed;
	private boolean deleted;
	private boolean compared;
	private FileCleaner fileCleaner;
	private SourceFile counterPart;

	public SourceFile(String filePath, String name, int version)
			throws IOException {
		this.filePath = filePath;
		this.version = version;
		this.name = name;

		fileCleaner = new FileCleaner();
		lines = new ArrayList<Line>();
		changeHeader = new ArrayList<ChangeHeader>();

		int lineCounter = 1;
		BufferedReader inputStream = null;
		StringBuffer sb = new StringBuffer();

		ArrayList<String> linesText = fileCleaner
				.cleanFileContents(this.filePath);

		String historicalData = "";
		String changeHeader = "";
		for (int i = 0; linesText != null && i < linesText.size(); i++) {

			if (linesText.get(i).matches(
					"\\[((Unchanged|Added|Removed|Modified)+\\{\\d+\\})+\\]")) {

				historicalData = linesText.get(i);

			} else if (linesText.get(i).matches("\\[.*\\]")) {

				changeHeader = linesText.get(i);
			} else {

				if (!historicalData.trim().equals("")) {

					Line l = new Line(lineCounter, linesText.get(i));
					l.setHistoricalData(historicalData);
					lines.add(l);

					historicalData = "";
				} else {

					lines.add(new Line(lineCounter, linesText.get(i)));
				}

				lineCounter++;
			}
		}
		locTotal = lines.size();

	}

	public String getName() {
		return name;
	}

	public String getFilePath() {
		return filePath;
	}

	public int getVersion() {
		return version;
	}

	public ArrayList<Line> getLines() {
		return lines;
	}

	public boolean getChanged() {
		return changed;
	}

	public boolean getDeleted() {
		return deleted;
	}

	public boolean getCompared() {
		return compared;
	}

	public void setChanged(boolean value) {
		changed = value;
	}

	public void setDeleted(boolean value) {
		deleted = value;
	}

	public void setCompared(boolean value) {
		compared = value;
	}

	public boolean addChangeHeader(ChangeHeader newChangeHeader) {

		changeHeader.add(newChangeHeader);

		return true;
	}

	public boolean addLine(Line newLine) {
		if (lines == null) {
			lines = new ArrayList<Line>();
		}
		lines.add(newLine);
		return true;
	}

	public boolean addLine(int index, Line newLine) {
		if (lines == null)
			return addLine(newLine);
		else
			lines.add(index, newLine);
		return true;
	}

	public SourceFile getCounterPart() {
		return counterPart;
	}

	public void setCounterPart(SourceFile file) {
		counterPart = file;
	}

	/**
	 * @return the locTotal
	 */
	public int getLocTotal() {
		return locTotal;
	}

	public String getChangeHeadersPrint() {

		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < changeHeader.size(); i++)
			sb.append(changeHeader.get(i).getChangeHeaderPrint());

		return sb.toString();

	}

	public String getChangeHeaderPrint(int version) {

		return "";

	}

	public String getFileChangeReport() {

		return "";
	}

	public String getFileListing() {

		return "";

	}

	public String getStadistictsModifiedFile() {

		return "";

	}

}
